package com.goods.controller.business;

import com.goods.business.service.InStockService;
import com.goods.common.response.ResponseBean;
import com.goods.common.utils.ListPageUtils;
import com.goods.common.vo.business.InStockItemVO;
import com.goods.common.vo.business.InStockVO;
import com.goods.common.vo.system.PageVO;
import io.swagger.annotations.Api;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "入库记录 ")
@RestController
@RequestMapping("business/inStock")
public class InStockController {
    @Resource
    private InStockService inStockService;

    @GetMapping("/findInStockList")
    public ResponseBean findInStockList(InStockVO inStockVO, Integer pageNum, Integer pageSize) {
        List<InStockVO> list = inStockService.findInStockList(inStockVO);
        List<InStockVO> page = ListPageUtils.page(list, pageSize, pageNum);

        return ResponseBean.success(new PageVO<>(list.size(),page));

    }

    @GetMapping("/detail/{id}")
    public ResponseBean detail(@PathVariable Long id,
                               Integer pageNum) {
        List<InStockItemVO> list = inStockService.findItemVOS(id);
        Integer total = list.size();
        if (!CollectionUtils.isEmpty(list)) {
            list = ListPageUtils.page(list, 4, pageNum);
        }
        InStockVO InStockVO = inStockService.findSupplierVO(id);
        Map map = new HashMap();
        map.put("itemVOS", list);
        map.put("total", total);
        map.put("supplierVO", InStockVO);
        map.put("status", InStockVO.getStatus());
        return ResponseBean.success(map);
    }

    //移入回收站
    @PutMapping("/remove/{id}")
    public ResponseBean remove(@PathVariable Long id) {
        Integer integer = inStockService.remove(id);
        if (integer > 0) {
            return ResponseBean.success();
        } else {
            return ResponseBean.error("移入回收站失败惹");
        }
    }

    //从回收站恢复
    @PutMapping("/back/{id}")
    public ResponseBean back(@PathVariable Long id) {
        Integer integer = inStockService.back(id);
        if (integer > 0) {
            return ResponseBean.success();
        } else {
            return ResponseBean.error("恢复失败惹");
        }
    }

    //删除
    @GetMapping("/delete/{id}")
    public ResponseBean update(@PathVariable Long id) {
        Integer integer = inStockService.delete(id);
        if (integer > 0) {
            return ResponseBean.success();
        } else {
            return ResponseBean.error("删除失败惹");
        }
    }

    //入库
    @PostMapping("/addIntoStock")
    public ResponseBean addIntoStock(@RequestBody InStockVO inStockVO) {
        Integer integer = inStockService.addIntoStock(inStockVO);
        if (integer > 0) {
            return ResponseBean.success();
        } else {
            return ResponseBean.error("添加失败惹");
        }
    }
    //审核
    @PutMapping("/publish/{id}")
    public ResponseBean publish(@PathVariable Long id){
        Integer integer = inStockService.publish(id);
        return ResponseBean.success();
    }
}
